package ss.lianft.controller;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.crypto.hash.Sha256Hash;
import org.apache.shiro.subject.Subject;
import org.beetl.sql.core.SQLManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import ss.lianft.model.Menu;
import ss.lianft.model.MenuView;
import ss.lianft.model.User;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/index")
public class IndexController extends BaseController {

    @Autowired
    private SQLManager sqlManager;

    @RequestMapping("login")
    public String loginPre(){
        return "login";
    }

    @RequestMapping(value="doLogin",method=RequestMethod.POST)
    public String login(@RequestParam("username") String username,@RequestParam("password") String password){
        Subject user = SecurityUtils.getSubject();
        if(!user.isAuthenticated()){
            UsernamePasswordToken token = new UsernamePasswordToken(username,password);
            try {
                user.login(token);
                return "forward:/index/index.btl";
            }catch (IncorrectCredentialsException ice) {
                logger.info("账户密码不匹配！");
                return "forward:/index/login.btl";
            }
        }else{
            return "forward:/index/index.btl";
        }

    }

    @RequestMapping("index")
    public String index(Model model){
        //Menu装配
        List<MenuView> menus = menus();
        Subject subject = SecurityUtils.getSubject();
        String username = (String)subject.getPrincipal();
        User user = new User();
        user.setUsername(username);
        user = sqlManager.selectUnique("user.list",user,User.class);
        model.addAttribute("menus",menus);
        model.addAttribute("user",user);
        return "index";
    }

    private List<MenuView> menus(){
        Subject user = SecurityUtils.getSubject();
        //页面显示的一级菜单
        List<MenuView> menus = new ArrayList();
        Menu menuQuery = new Menu();
        menuQuery.setParentId(0);
        List<Menu> srcMenus = sqlManager.select("menu.list",Menu.class,menuQuery);
        for(Menu menu : srcMenus){
            if(user.isPermitted(String.valueOf(menu.getMenuFunc()))){
                //存在权限
                MenuView menuView = new MenuView();
                menuView.setMenuFunc(menu.getMenuFunc());
                menuView.setMenuName(menu.getMenuName());
                menuView.setMenuText(menu.getMenuText());
                menuView.setId(menu.getId());
                //检索二级菜单
                menuQuery.setParentId(menuView.getId());
                List<Menu> childSrcMenus = sqlManager.select("menu.list",Menu.class,menuQuery);
                List<Menu> childMenus = new ArrayList();

                for(Menu childMenu : childSrcMenus){
                    if(user.isPermitted(String.valueOf(childMenu.getMenuFunc()))){
                        childMenus.add(childMenu);
                    }
                }
                menuView.setSubMenu(childMenus);
                menus.add(menuView);
            }

        }
        return menus;
    }

    @RequestMapping("/logout")
    public String logout(){
        SecurityUtils.getSubject().logout();
        return "login";
    }

    public static void main(String[] args) {
        System.out.println(new Sha256Hash("admin","admin").toString());
    }
}
